package com.organic.cell.http;

import com.organic.cell.http.internal.rest.FirstStepsApplication;
import com.organic.cell.http.internal.rest.HttpServerHelper;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
import org.restlet.Component;
import org.restlet.Server;
import org.restlet.data.Protocol;
import org.restlet.engine.Engine;

//@Component(name = "CellHTTP")
//@Instantiate
public class CellHTTP {
  private static final Logger LOG = Logger.getLogger(CellHTTP.class);

  // Configure the server.
  //    private static final ServerBootstrap bootstrap = new ServerBootstrap(new NioServerSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool()));

  //    @Validate
  public void validate() throws Exception {
    //        // Set up the event pipeline factory.
    //        bootstrap.setPipelineFactory(new HttpServerPipelineFactory());
    //
    //        // Bind and start to accept incoming connections.
    //        bootstrap.bind(new InetSocketAddress(8080));

    //        org.restlet.ext.netty.HttpServerHelper test;
    //        NettyServerHelper helper = new HttpServerHelper(new Server(Protocol.HTTP, HelloWorldResource.class));

    // Start the component.
    //        component.start();

    LOG.info("CellHTTP validated");
  }

  //    @Invalidate
  public void invalidate() {
    LOG.info("CellHTTP invalidated");
  }

  public static void main(String[] args) throws Exception {
    BasicConfigurator.configure();
    HttpServerHelper helper = new HttpServerHelper(new Server(Protocol.HTTP));
    Engine.getInstance().getRegisteredServers().clear();
    Engine.getInstance().getRegisteredServers().add(helper); // Add it first

    // Server creation
    Component component = new Component();
    component.getServers().add(Protocol.HTTP, 8080);
    // Attach the sample application.
    component.getDefaultHost().attach("/test", new FirstStepsApplication());
    component.start();
  }
}
